package br.com.pucrs.tcc.bd;

import java.util.ArrayList;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.com.pucrs.tcc.ed.AreaInteresse;
import br.com.pucrs.tcc.ed.Telefone;

public class TelefoneBD {

	private Session mSession;

	public TelefoneBD(Session session) {
		mSession = session;
	}

	public void insert(Telefone telefone) {
		mSession.save(telefone);
	}

	public void update(Telefone telefone) {
		mSession.update(telefone);
	}

	public void delete(Telefone telefone) {
		mSession.delete(telefone);
		mSession.flush();
	}

	public Telefone retrieve(Telefone telefone) {
		Telefone telefoneRetorno = null;
		
		Criteria criteria = mSession.createCriteria(Telefone.class);
		criteria.add(Restrictions.eq("id", telefone.getId()));
		
		telefoneRetorno = (Telefone) criteria.uniqueResult();	
		
		return telefoneRetorno;
	}
	
	public ArrayList<Telefone> list(Telefone area) {
		ArrayList<Telefone> listaRetorno = null;
		
		Criteria criteria = mSession.createCriteria(AreaInteresse.class);
		//criteria.add(Restrictions.eq("matricula", professor.getMatricula()));
		
		listaRetorno = (ArrayList<Telefone>) criteria.list();	
		
		if (listaRetorno != null) {
			System.out.println(listaRetorno.size());
		}
		else {
			System.out.println("AREA.NULL");
		}
		
		return listaRetorno;
	}
	
	public void removerPorUsuario(String matricula) {
		StringBuilder sql = new StringBuilder();
		sql.append("DELETE FROM telefones ");
		sql.append(" WHERE usuarios_matricula = ? ");

		SQLQuery query = mSession.createSQLQuery(sql.toString());
		query.setString(0, matricula);

		query.executeUpdate();
	}

}
